package com.qcq.wecheck.controller.auth;

import com.qcq.wecheck.entity.Auth;
import com.qcq.wecheck.result.CommonResult;
import com.qcq.wecheck.service.AuthService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 权限验证控制器
 * </p>
 *
 * @author O
 * @version 1.0
 * @since 2018/9/17
 */
@RestController
@RequestMapping("/auth")
public class AuthController {
	@Resource
	private AuthService authService;

	/**
	 * 根据openId注册一个随机密码sk, 每次请求都根据ak/sk验证
	 *
	 * @param auth 注册信息
	 * @return json
	 */
	@PostMapping
	public CommonResult register(@Validated Auth auth, BindingResult bindingResult) {
		if (!bindingResult.hasErrors()) {
			if (authService.insert(auth)) {
				return CommonResult.success();
			}
			return CommonResult.failure("accessKey已经存在");
		}
		return CommonResult.failure(bindingResult.getFieldError().getDefaultMessage());
	}
}
